import Observer from "./TodoList/Observer";
import { ITodoData } from "./types/typings";
; ((doc) => {
    const oTodoList: HTMLElement = doc.querySelector('.todo-list');
    const oBtn: HTMLElement = doc.querySelector('.add-btn');
    const oInput: HTMLInputElement = doc.querySelector('.todo-input');
    const observer = Observer.create(oTodoList);
    const init = (): void => {
        bindEvent();
    }
    function bindEvent() {
        oBtn.addEventListener('click', handleBtnClick, false);
        oTodoList.addEventListener('click', handleTodoListClick, false);
    }
    function handleBtnClick() {
        const val: string = oInput.value.trim();
        if (!val.length) {
            return;
        }
        observer.notify<ITodoData>('add', {
            id: new Date().getTime(),
            content: val,
            completed: false
        });
        oInput.value = '';
    }
    function handleTodoListClick(e: MouseEvent) {
        const tar: HTMLElement = e.target as HTMLElement;
        const tagName = tar.tagName.toLowerCase();
        const id: number = parseInt(tar.dataset.id);
        switch (tagName) {
            case 'input':
                observer.notify<number>('toggle', id);
                break;
            case 'button':
                observer.notify<number>('remove', id);
                break;
            default:
                break;
        }
    }
    init();
})(document);